// Virtual PoleView.h : interface of the CVirtualPoleView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_VIRTUALPOLEVIEW_H__A87F3F82_4FBD_4C74_BFF8_62794B7D028F__INCLUDED_)
#define AFX_VIRTUALPOLEVIEW_H__A87F3F82_4FBD_4C74_BFF8_62794B7D028F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "MainFrm.h"
#include "niimaq.h"
#include "SetROI.h"			
#include "SetMesurement.h"	
#include "SetPole.h"		
#include "SetThreshold.h"	
#include "SetProperties.h"	

class CVirtualPoleView : public CView
{
protected: // create from serialization only
	CVirtualPoleView();
	DECLARE_DYNCREATE(CVirtualPoleView);

// Attributes
public:
	CVirtualPoleDoc* GetDocument();

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CVirtualPoleView)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	protected:
	//}}AFX_VIRTUAL

// Implementation
public:
	void OnUpdateFPSStatus(CCmdUI *pCmdUI);
	CRITICAL_SECTION	*m_csRawVideo;
	CRITICAL_SECTION	*m_csAnalyzedVideo;
	virtual ~CVirtualPoleView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:
// Generated message map functions
protected:
	afx_msg void onDisableWhenAnalyzing(CCmdUI* pCmdUI);

	//{{AFX_MSG(CVirtualPoleView)
	afx_msg int  OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnViewVirtualPole();
	afx_msg void OnDataLoaded(CCmdUI* pCmdUI);
	afx_msg void OnUpdateViewVirtualPole(CCmdUI* pCmdUI);
	afx_msg void OnSettingsSetMesurmentUnits();
	afx_msg void OnSettingsSetROI();
	afx_msg void OnSettingsSetThresholds();
	afx_msg void OnSettingsSetVirtualPole();
	afx_msg void OnSettingsSetProperties();
	afx_msg void OnFileCloseVideo();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnToolBarSetRoi();
	afx_msg void OnToolBarSetMesurmentUnits();
	afx_msg void OnToolBarSetVP();
	afx_msg void OnViewKeepaspectratio();
	afx_msg void OnUpdateViewKeepaspectratio(CCmdUI* pCmdUI);
	afx_msg void OnUpdateFrameNum(CCmdUI* pCmdUI);
	afx_msg void OnFileExit();
	afx_msg void OnAnalyzeStart();
	afx_msg void OnViewSourceVideo();
	afx_msg void OnUpdateViewSourceVideo(CCmdUI* pCmdUI);
	afx_msg void OnUpdateViewAnalyzedVideo(CCmdUI* pCmdUI);
	afx_msg void OnViewAnalyzedVideo();
	afx_msg void OnViewFrameNumbers();
	afx_msg void OnUpdateViewFrameNumbers(CCmdUI* pCmdUI);
	afx_msg void OnUpdateAnlyzeStart(CCmdUI* pCmdUI);
	afx_msg void OnUpdateAnalyzeStop(CCmdUI* pCmdUI);
	afx_msg void OnAnalyzeStop();
	afx_msg void OnUpdateFileStartVideoSession(CCmdUI* pCmdUI);
	afx_msg void OnListen();
	afx_msg void OnAnalyzeSave();;
	afx_msg void OnAnalyzeBtnSave();
	afx_msg void OnUpdateAnalyzeSave(CCmdUI* pCmdUI);
	afx_msg void OnUpdateFileListenOnExternalPort(CCmdUI* pCmdUI);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP();
private:
	void OnUpdateTriggerStatus(CCmdUI *pCmdUI);
	void OnUpdateSaveStatus(CCmdUI *pCmdUI);
	void OnUpdateNetworkStatus(CCmdUI *pCmdUI);
	void RefreshAnalyzeParams();

	//A SOCKET is simply a typedef for an unsigned int.
    //In Unix, socket handles were just about same as file 
    //handles which were again unsigned ints.
    //Since this cannot be entirely true under Windows
    //a new data type called SOCKET was defined.
    SOCKET m_socServer;

	Analyze_Params* m_pAnalyze_Params; // parameters for analyzing
	Settings_Params m_Settings;		   // program params
	Settings_Params	m_Settings_backup;

	// image containment variables
	CBitmap	m_bmGreenCross, m_bmRedCross, m_bmSourceFrame, m_bmAnalyzedFrame;
	//CDC		m_memDCGreenCross, m_memDCRedCross;
	CDC		m_memDcSourceFrame, m_memDcAnalyzedFrame;
	
	// dialogs variables
	CSetMesurement	m_dSetMesurement;
	CSetThreshold	m_dSetThreshold;
	CSetPole		m_dSetPole;
	CSetROI			m_dSetROI;
	CSetProperties	m_dSetProperties;

	//GUI flags
	bool m_bKeepMode, m_bOriginSet;
	
	// status flags
	bool m_bData_Loaded, m_bAnalyzing, m_bListening, m_bClosing, m_bParamsSaved;

	// GUI variables
	CPoint	m_ptOrigin, m_ptCurrent, m_ptPrevious;	// Points on the screen
	dPoint	m_StrechFactor;					// Video stretch factor
	CRect	m_rcPrev, m_rcCurr, m_rcVideo;
	CSize	m_RectSize,m_imSize; 
	
	byte	m_nMode; // 0 - no mode, 1 - set ROI, 2 - set pixel/mm ratio, 3 - set the Virtual Pole location
	
	// computational
	Result_Params	m_Results;				// Returned Results from document Class
	unsigned int	m_iRawBufferNum;		// the current Raw buffer number
	unsigned int	m_iAnalyzedBufferNum;	// the current Analyzed buffer number
	unsigned int	m_iRawBufferSize;		// the size of the raw buffer in frames
	unsigned int	m_iAnalyzedBufferSize;	// the size of the analyzed buffers in frames		
	CWinThread		* m_hVideoThread;		// a thread to show the video
	int				m_iError;				// Imaq error number
	CRect			VPLocation;				// location and size of the active virtual pole
	CRect			AllVPLocations[10];		// location and size of all virtual objects
	POINT			m_PointsArray[1024];	// array that holds whisker median trace in screen pixel coordinates
	POINT			m_AnglePointsArray[1024];	// array that holds whisker angle trace in screen pixel coordinates
	POINT			m_FFTWArray[256];
	unsigned short	m_iNumPoints;
	CString			m_csSignalNoise;
	unsigned long	m_LastAnalyzed;

	// functions
	CRect			ResizeVideoAspectRatio(CSize imSize, bool bSourceVideo);
	dPoint			Screen2Frame(CPoint CurrLoc);
	void			OnUpdateGrid(CCmdUI* pCmdUI);
	void			ShowVideoProc(int RefreshTime);
	void			DisplayImaqError(int error);
	void			ServerProc();
	static UINT		VideoThread(LPVOID pParam);
	static UINT		ServerThread(LPVOID pParam);
	POINT			m_ptMaskVertices[100];	// points for motion mask, max 100 points
	int				m_iVertexCounter;
	CRgn			m_crMotionMask;
public:
	afx_msg void OnFileStartCaptureVideo();
	afx_msg void OnViewTriggerIndicatorOverlays();
	afx_msg void OnUpdateViewTriggerIndicatorOverlays(CCmdUI *pCmdUI);
	afx_msg void OnUpdateHelpOnlineHelp(CCmdUI *pCmdUI);
	afx_msg void OnHelpOnlineHelp();
	afx_msg void OnHelpProjectHomepage();
	afx_msg void OnUpdateHelpProjectHomepage(CCmdUI *pCmdUI);
	afx_msg void OnUpdateHelpReportBug(CCmdUI *pCmdUI);
	afx_msg void OnHelpReportBug();
private:
	bool m_bNoROIModal;
public:
	afx_msg void OnViewRTFFT();
	afx_msg void OnUpdateViewRTFFT(CCmdUI *pCmdUI);
	afx_msg void OnViewRTTRACKING();
	afx_msg void OnUpdateViewRTTRACKING(CCmdUI *pCmdUI);
	afx_msg void OnControlCaptureDarkFrame();
	afx_msg void OnUpdateControlCaptureDarkFrame(CCmdUI *pCmdUI);
	afx_msg void OnViewSetMotionMask();
};

#ifndef _DEBUG  // debug version in Virtual PoleView.cpp
inline CVirtualPoleDoc* CVirtualPoleView::GetDocument()
   { return (CVirtualPoleDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_VIRTUALPOLEVIEW_H__A87F3F82_4FBD_4C74_BFF8_62794B7D028F__INCLUDED_)
